<?php
namespace Umpmobile\Model;
use Think\Model;
class FriendsModel extends Model{
	public $friend;
	public $sid;
	public $cid;
	public $id;
	public $friend_info;
	public $friend_id;
	public $save_data;
	public $last_promo_id;
	public $last_decrease_amount;
	public function getFriend(){
		$data["mixed_nick"] = $this->friend;
		$data["sid"] = $this->sid;
		$friend = current($this->where($data)->getField("`id`,`friends`,`join_time`,`friend_link`,`qrcode_url`,`short_url`,`last_promo_id`,`last_decrease_amount`"));
		// print_r($friend);
		if(!$friend){
			$id = $this->addFriend();
		}else{
			$id = $friend["id"];
		}
		$friend["sid"]=$this->sid;
		$friend["mixed_nick"] = $this->friend;
		$this->friend_info = $friend;
		return $id;
	}
	public function addFriend(){
		$data["friends"] = 0;
		$data["mixed_nick"] = $this->friend;
		$data["sid"] = $this->sid;
		$data["join_time"] = date("Y-m-d H:i:s");
		$data["friend_link"] = "https://hongbao.ews.m.jaeapp.com/?friendnick=".$this->friend."&sid=".$this->sid."&cid=".$this->cid;
		$this->friend_info = $data;
		return $this->add($data);
	}
	public function saveFriend(){
		// return $this->save($this->save_data);
		return $this->where("`id`=".$this->friend_id." AND `sid`=".$this->save_data["sid"])->save($this->save_data);
	}
	public function friendsUp(){
		$data["sid"] = $this->sid;
		$data["id"] = $this->id;
		return $this->where($data)->setInc("friends");
	}
	public function lastPromSave(){
		$data["sid"] = $this->sid;
		$data["id"] = $this->id;
		$save["last_decrease_amount"]=$this->last_decrease_amount;
		$save["last_promo_id"]=$this->last_promo_id;
		return $this->where($data)->save($save);
	}
}